package com.stone.framework.workflow.client;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;

import feign.hystrix.FallbackFactory;

@FeignClient(name = "stone-user-service", fallbackFactory = UserClient.UserFallbackFactory.class)
public interface UserClient {

    Logger LOGGER = LoggerFactory.getLogger(UserClient.class);

    @RequestMapping(value = "/user/getByRole", method = RequestMethod.POST)
    ResMessage<List<String>> getByRole(ReqMessage<String> reqMessage);

    @Cacheable(value = "TEST", key = "#role", unless = "#result == null") // TODO
    default List<String> getByRole(String role) {
        ResMessage<List<String>> resMessage = this.getByRole(ReqMessage.build(role));
        return resMessage.getBody();
    }

    @Component
    public static class UserFallbackFactory implements FallbackFactory<UserClient> {

        @Override
        public UserClient create(Throwable cause) {
            return new UserClient() {

                @Override
                public ResMessage<List<String>> getByRole(ReqMessage<String> reqMessage) {
                    LOGGER.error("getUsersByRole()调用异常", cause);
                    return ResMessage.success(null);
                }

            };
        }

    }

}
